package com.shopping.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.shopping.commons.response.DataGradResp;
import com.shopping.commons.response.MessageResp;
import com.shopping.commons.util.CommonCode;
import com.shopping.mapper.AssessMapper;
import com.shopping.model.Assess;
import com.shopping.param.AssessParam;
import com.shopping.response.AssessResp;
import com.shopping.service.IAssessService;

@Service    //这样写的话就会默认这个bean的名字是    addressService
public class AssessService implements IAssessService{
	
	@Autowired
	private AssessMapper assessMapper;
	
	private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public MessageResp insert(Assess param) {
		MessageResp resp=new MessageResp();
		param.setDelflag("0");
		param.setCreatedate(sdf.format(new Date()));
		if(assessMapper.insertSelective(param)>0){
			resp.setCode(CommonCode.SUCCESS);
			resp.setMsg("添加成功");
		}else{
			resp.setCode(CommonCode.FAIL);
			resp.setMsg("添加失败");
		}
		return resp;
	}

	public MessageResp update(Assess param) {
		// TODO Auto-generated method stub
		return null;
	}

	public MessageResp delete(Integer param) {
		// TODO Auto-generated method stub
		return null;
	}

	public DataGradResp<Assess> select(Assess param) {
		// TODO Auto-generated method stub
		return null;
	}

	public MessageResp batchDelete(String ids) {
		// TODO Auto-generated method stub
		return null;
	}

	public MessageResp selectById(Integer id) {
		MessageResp resp=new MessageResp();
		AssessParam param=new AssessParam();
		param.setId(id);
		param.setDelflag("0");
		List<AssessResp> retList=null;
		try {
			retList=assessMapper.select(param);
		} catch (Exception e) {
			resp.setCode(CommonCode.FAIL);
			resp.setMsg("查询失败");
			return resp;
		}
		resp.setCode(CommonCode.SUCCESS);
		resp.setMsg("查询成功");
		resp.setObj(retList);
		return resp;
	}

	public MessageResp add(Integer userId,Integer orderid, Integer commid, String content) {
		MessageResp resp=new MessageResp();
		if(StringUtils.isEmpty(content)){
			resp.setCode(CommonCode.FAIL);
			resp.setMsg("请正确填写评价");
			return resp;
		}
		AssessParam param=new AssessParam();
		param.setCommid(commid);
		param.setOrderid(orderid);
		param.setDelflag(CommonCode.DELFLAG_NORMAL);
		List<AssessResp> retList=assessMapper.select(param);
		if(retList!=null&&!retList.isEmpty()){
			//修改
			Assess param1=new Assess();
			param1.setCommid(commid);
			param1.setOrderid(orderid);
			param1.setDelflag(CommonCode.DELFLAG_NORMAL);
			param1.setContent(content);
			param1.setUpdatedate(sdf.format(new Date()));
			param1.setId(retList.get(0).getId());
			if(assessMapper.update(param1)>0){
				resp.setCode(CommonCode.SUCCESS);
				resp.setMsg("评价成功");
			}else{
				resp.setCode(CommonCode.FAIL);
				resp.setMsg("评价失败");
			}
		}else{
			Assess param1=new Assess();
			param1.setCommid(commid);
			param1.setOrderid(orderid);
			param1.setDelflag(CommonCode.DELFLAG_NORMAL);
			param1.setContent(content);
			param1.setCreatedate(sdf.format(new Date()));
			Random r=new Random();
			param1.setId(r.nextInt(10000)+100000);
			if(assessMapper.insertSelective(param1)>0){
				resp.setCode(CommonCode.SUCCESS);
				resp.setMsg("评价成功");
			}else{
				resp.setCode(CommonCode.FAIL);
				resp.setMsg("评价失败");
			}
		}
		return resp;
	}
}
